package jitobject;

import java.io.*;
import java.nio.charset.StandardCharsets;

import fileoperation.FileCreation;
import fileoperation.FileDeletion;
import repository.Repository;
import zlib.ZLibUtils;


public class JitObject implements Serializable {

    protected String fmt;                  //type of object
    protected String key;                  //key of object
    protected String mode;                 //mode of object
    protected static String path = Repository.getJitDir() + File.separator + "objects";          //absolute path of objects
    protected String value;                //value of object

    public String getFmt() {
        return fmt;
    }

    public String getKey() {
        return key;
    }

    public String getMode() {
        return mode;
    }

    /**
     * return path of .jit/Objects
     *
     * @param
     * @throws Exception
     */
    public String getObjectsPath() {
        return path;
    }

    public String getValue() {
        return value;
    }

    public JitObject() {
    }

    /**
     * Get the value(content) of file
     *
     * @param file
     * @return String
     * @throws IOException
     */
    public static String getValue(File file) throws IOException, Exception {
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            line = bufferedReader.readLine();
            while (line != null) {
                stringBuilder.append(line);
                stringBuilder.append(System.lineSeparator());
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            inputStreamReader.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return String.valueOf(stringBuilder);
    }

    /**
     * Serialize jitObject and compress in path.
     *
     * @throws Exception
     */
    public void compressWrite(String path, JitObject jitObject) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(jitObject);
        byte[] data = byteArrayOutputStream.toByteArray();
        File file = new File(path);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ZLibUtils.compress(data, fileOutputStream);
    }

    /**
     *
     *
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     *  Deserialize
     */
    public static JitObject deserialize(File file) throws IOException, ClassNotFoundException {
        //File file = new File(Path);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] content = ZLibUtils.decompress(fileInputStream);
        InputStream inputStream = new ByteArrayInputStream(content);

        ObjectInputStream obj = new ObjectInputStream(inputStream);

        return (JitObject) obj.readObject();
    }

}
